package com.wd.health.view.activity.mine;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.gyf.immersionbar.ImmersionBar;
import com.wd.health.R;
import com.wd.health.model.bean.MessageStatusBean;
import com.wd.health.model.bean.YqmBean;
import com.wd.health.utils.RetrofitManager;
import com.wd.health.utils.ZXingUtils;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class InviteFriendActivity extends AppCompatActivity {

    private TextView tv_yqm;
    private ImageView img_erweima;
    private ImageView img_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invite_friend);
        ImmersionBar.with(this).init();
        initView();
        initInvite();
        initData();
    }



    private void initView() {
        tv_yqm = findViewById(R.id.tv_yqm);
        img_erweima = findViewById(R.id.img_erweima);
        img_back = findViewById(R.id.img_back);
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initInvite() {
        RetrofitManager.getInstance().postYqmData(new Observer<MessageStatusBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(MessageStatusBean messageStatusBean) {
                if (messageStatusBean.getStatus().equals("0000")){
                    Toast.makeText(InviteFriendActivity.this, "生成邀请码成功", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void initData() {
        RetrofitManager.getInstance().getYHYqmData(new Observer<YqmBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(YqmBean yqmBean) {
                Log.e("TAG", "获取邀请码:"+yqmBean.getMessage().toString());
                String result = yqmBean.getResult();
                tv_yqm.setText(result);
                Bitmap image = ZXingUtils.createQRImage(tv_yqm.getText().toString(), 150, 150);
                img_erweima.setImageBitmap(image);
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }


}